<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="detailsList">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="品牌名称：">
              <span>{{ props.row.equipment.name }}</span>
            </el-form-item>
            <el-form-item label="产品型号：">
              <span>{{ props.row.equipment.model }}</span>
            </el-form-item>
            <el-form-item label="产品参数：">
              <span>{{ props.row.equipment.parameter }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="类型名称" align="center" prop="typeId" :formatter="typeFormat" />
      <el-table-column label="品牌名称" align="center" prop="brandId" :formatter="brandFormat" />
      <el-table-column label="产品名称" align="center" prop="equipment.name" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="购买时单价" align="center" prop="price" />
      <el-table-column label="购买时成本单价" align="center" prop="priceCost" />
      <el-table-column label="单价合计" align="center" prop="priceTotal" />
      <el-table-column label="成本合计" align="center" prop="priceTotalCost" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:details:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数量" prop="count">
          <el-input-number v-model="form.count" :min="1" label="请输入数量" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDetailsTSXUWJ,
  getDetails,
  delDetailsTSXUWJ,
  addDetails,
  updateDetailsTSXUWJ,
  exportDetails
} from "@/api/system/details";
import { listType } from "@/api/system/type";
import { listBrand } from "@/api/system/brand";

export default {
  name: "Details",
  props: {
    order: {}
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      detailsList: [],
      //
      typeOptions: [],
      //
      brandOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        count: [{ required: true, message: "数量不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    listType().then(response => {
      this.typeOptions = response.rows;
    });
    listBrand().then(response => {
      this.brandOptions = response.rows;
    });
  },
  methods: {
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      this.queryParams.orderId = this.order.id;
      listDetailsTSXUWJ(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderId: undefined,
        equipmentId: undefined,
        count: undefined,
        price: undefined,
        priceCost: undefined,
        priceTotal: undefined,
        priceTotalCost: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDetailsTSXUWJ(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.$emit("getOrder", this.form.orderId);
              }
            });
          } else {
            addDetails(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除订单详情编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDetailsTSXUWJ(ids);
        })
        .then(() => {
          this.getList();
          this.$emit("getOrder", row.orderId);
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单详情数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDetails(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    brandFormat(row, column) {
      var brandName = undefined;
      this.brandOptions.forEach(element => {
        if (element.id == row.equipment.brandId) {
          brandName = element.name;
          return;
        }
      });
      return brandName;
    },
    typeFormat(row, column) {
      var typeName = undefined;
      this.typeOptions.forEach(element => {
        if (element.id == row.equipment.typeId) {
          typeName = element.name;
          return;
        }
      });
      return typeName;
    }
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}
</style>